package org.etcsoft.examples.saniker;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

public class FileTreeLabelProvider extends LabelProvider {
	private Map imageCache = new HashMap(11);
	
	public Image getImage(Object element) {
		ImageDescriptor descriptor = null;
		
	    if(((File) element).isDirectory())
	    {
	    	if(((File) element).getParent() == null)
	    	{
	    		descriptor = ImageDescriptor.createFromFile(this.getClass(), "icon/icon_ClosedDrive.gif");
	    	} else 
	    	{
	    		descriptor = ImageDescriptor.createFromFile(this.getClass(), "icon/icon_ClosedFolder.gif");	
	    	}
	    	
	    } else {
	    	descriptor = ImageDescriptor.createFromFile(this.getClass(), "icon/icon_File.gif");
	    }
	    
	    Image image = (Image) imageCache.get(descriptor);
	    
		if (image == null) {
			image = descriptor.createImage();
			imageCache.put(descriptor, image);
		}
		
		return image;
	}

    public String getText(Object element) {
    	
        String name = ((File) element).getName();
        
        if(name.isEmpty()) name = ((File) element).getPath();
        
        return name;
    } 
}
